<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct(Request $request)
    {

    }
    /**
     * @param Request $request
     * @return string|null
     *
     * FileUpload
     */
    public function FileUpload(Request $request): ?string
    {
//        $user = $request->get('user');
//        $username = $user['username'];
//        $path_date='app/public/files/'.date('Ymd');
//        if(!file_exists(storage_path($path_date))){
//            mkdir(storage_path($path_date),0777,true);
//        }
//        $save_path='public/files/'.date('Ymd').'/'.$username;
        $save_path='files/'.date('Ymd');//.'/'.$username;
        Log::info('save_path:'.$save_path);
        $path = $save_path;
        if(!file_exists(storage_path($path))){
            mkdir(storage_path($path),0777,true);
        }
        $file = $request->file('file');
        $fileName = $request->get('name');
        $ext = $file->getClientOriginalExtension();// 扩展名
//        $fileName = md5($fileName). '.' .$ext;
        $path = $file->getRealPath();// 临时绝对路径
//        dd($request->all());
        $filePath = $save_path . '/' .$fileName;//文件路径
//        if(Storage::disk('public')->exists($filePath)){
//            return response()->json([
//                'code' => 200,
//                'result' => $filePath,
//                'msg'=>'file is exists'
//            ]);
//        }
        $res = Storage::put($filePath,file_get_contents($file));// file_get_contents: 将整个文件读入一个字符串
        $result = null;
        if ($res) {
            $result = 'public/'.$filePath;
        }
        return $result;
    }

    public function FileUploadNew(Request $request){

        $file = $request->file('file');
        $old_name = $file->getClientOriginalName();//文件原名
        $ext = $file->getClientOriginalExtension();     // 扩展名
        $type = $file->getClientMimeType();     // image/jpeg
        $path = $file->storeAs('file/'.date('Y-m-d'), time().rand(0,99999).'.jpg');
        if($path){
            $result =  "/storage/".$path;
            return $result;
        }
        return '';
    }

    public function FileUploadNewBack(Request $request){
        //        $user = $request->get('user');
//        $username = $user['username'];
//        $path_date='app/public/files/'.date('Ymd');
//        if(!file_exists(storage_path($path_date))){
//            mkdir(storage_path($path_date),0777,true);
//        }
//        $save_path='public/files/'.date('Ymd').'/'.$username;
        $save_path='files/'.date('Ymd');
//        $save_path='files';
        Log::info('save_path:'.$save_path);
        $path = $save_path;
        if(!file_exists(storage_path($path))){
            mkdir(storage_path($path),0777,true);
        }
        $file = $request->file('file');
        $fileName = $request->get('name');
        $ext = $file->getClientOriginalExtension();// 扩展名
//        $fileName = md5($fileName). '.' .$ext;
        $path = $file->getRealPath();// 临时绝对路径
//        dd($request->all());
        $filePath = $save_path . '/' .$fileName;//文件路径
//        if(Storage::disk('public')->exists($filePath)){
//            return response()->json([
//                'code' => 200,
//                'result' => $filePath,
//                'msg'=>'file is exists'
//            ]);
//        }
        $res = Storage::put($filePath,file_get_contents($file));// file_get_contents: 将整个文件读入一个字符串
        $result = null;
        if ($res) {
            $result = $filePath;
        }
        return $result;
    }

    public function FileUploadApi(Request $request){

        $file = $request->file('file');
        $old_name = $file->getClientOriginalName();//文件原名
        $ext = $file->getClientOriginalExtension();     // 扩展名
        $type = $file->getClientMimeType();     // image/jpeg
        $path = $file->storeAs('file/'.date('Y-m-d'), time().rand(0,99999).'.jpg');
        if($path){
            $data = array(
                'code' => 200,
                'msg'  => '上传成功',
                'data' => array(
                    'src'   => "/storage/".$path,
                    'old_name' => $old_name,
                    'ext' => $ext,
                    'type' => $type
                ),
            );
        }else{
            $data = array(
                'code' => 400,
                'msg'  => '上传失败',
                'data' => array(
                ),
            );
        }
        return json_encode($data);
    }
    public function common(){
//        shell_exec('php artisan down');
        $up = \request('up',2);
        if($up == 1){
            Artisan::call('up');
        }else{
            Artisan::call('down');
        }
    }
}
